Zepto(function($) {
  window.onload=function () {
    getCartList();
  }
  // location.reload();
  var selectShopCartsIdList = [];
  /**
   *
   * 查询购物车列表
   *
   */
  function getCartList() {
    // $.ajax({
    //   type: 'GET',
    //   url: getUrl + '/wxmall/shopcarts/list',
    //   data: {
    //     // memberId:memberId
    //   },
    //   dataType: 'json',
    //   timeout: 15000,
    //   headers: headers("shopcarts.list"),
    //   context: $('body'),
    //   success: function(data){
    //     getCartListInit(data);
    //   },
    //   error: function(xhr, type){
        cartNoGoods();
    //   }
    // });
  }
  function cartNoGoods() {
    $(".content").css("display","none");
    $(".cart-nogoods").css("display","block");
  }
  /**
   * 初始化购物车列表
   *
   * @param data
   */
  function getCartListInit(data) {
    console.log(data);
    $("#list").html("");
    if(!data.success){
      cartNoGoods();
      return
    }
    if(data.data.length == 0){
      cartNoGoods();
      return
    }
    var data1 = data.data;
    if(data1 != null && data1 != undefined && data1 != ""){
      var html = "";
      for(var i = 0; i < data1.length; i++){
        var productName = "";
        if(data1[i].productName != null){
          productName = data1[i].productName;
        }
        var num = "";
        if(data1[i].num != null){
          num = data1[i].num;
        }
        var norms = "";
        if(data1[i].norms != null){
          norms = data1[i].norms;
        }
        var shopCartsId = "";
        if(data1[i].shopCartsId != null){
          shopCartsId = data1[i].shopCartsId;
        }
        var picUrl = "";
        if(data1[i].picUrl != null){
          picUrl = imgUrl + data1[i].picUrl;
        }
        var specificationId = "";
        if(data1[i].specificationId != null){
          specificationId = data1[i].specificationId;
        }
        var price = "";
        if(data1[i].price != null){
          price = "￥" + data1[i].price;
        }
        var limitPrice = "";
        var limitPriceall = ""
        if(data1[i].limitPrice != null){
          limitPrice = "￥" + data1[i].limitPrice;
          limitPriceall = data1[i].limitPrice;
        }
        // var count1 = parseInt(count);
        // var prices = parseInt(data1[i].price);
        // var limitpri = parseInt(data1[i].limitPrice);
        // var limitPriceall = count1*limitpri;
        // var priceAll = count1*prices;
        html += '<li data-limitPrice="'+limitPriceall+'" data-price = "'+ data1[i].price +'" class="mui-table-view-cell">'+
        '<div shopcarts-id="'+shopCartsId+'" class="checkbox">'+
        '<input type="checkbox">'+
        '<label></label>'+
        '</div>'+
        '<div class="goods-image">'+
        '<img src="'+picUrl+'">'+
        '</div>'+
        '<div class="goods-detail">'+
        '<div class="goods-title">'+ productName +'</div>'+
        '<div class="goods-sku">'+norms+'</div>'+
        '<div class="select-num-content">'+
        '<div class="add-num">-</div><div data-specificationId="'+specificationId+'" class="value-num">'+ num +'</div><div class="add-add">+</div>'+
        '</div>'+
        '<div data-shopCartsId="'+shopCartsId+'" class="goods-delete"></div>'+
        '<div class="goods-price">'+
        '<span class="y-price">'+limitPrice+'</span>'+
        '<span class="s-price">'+ price +'</span>'+
        '</div>'+
        '</div>'+
        '</li>';
      }
      $("#list").append(html);
    }
  }

  /**
   * 返回首页
   */
  $(".cart-nogoods").on("click",".nogoods-find-btn",function () {
    window.location.href = "/index/index.html";
  })
  /**
   * 返回上一页
   */
  $(".nav-header").on("click",".nav-edit",function () {
    // window.history.go(-1);
    window.history.back(-1);
  });
  /**
   * 全选
   */
  $("#allSelect").click(function () {
    selectShopCartsIdList = [];
    //更改状态为全选
    var array = $("#list").find("input");
    var check = $(this).find("input")[0].checked;
    array.forEach(function (item,index,array) {
      item.checked = check;
    });
    $(this).find("input")[0].checked = check;
    //计算合计
    var arrayprice = $("#list").find(".mui-table-view-cell");
    var allprice = parseInt(0);
    var allLimitprice = parseInt(0);
    arrayprice.forEach(function (item,index,array) {
      $(item).find(".value-num");
      var shopcartsId = $(item).find(".checkbox")[0].getAttribute("shopcarts-id");
      selectShopCartsIdList.push(shopcartsId);
      var count =parseInt($(item).find(".value-num")[0].innerHTML);
      var privce = parseInt(item.getAttribute("data-price"));
      var limitPrice = parseInt(item.getAttribute("data-limitPrice"));
      allprice += parseInt(count*privce);
      allLimitprice += count*limitPrice;
    });
    if(!isNaN(parseInt(allprice))){
      $("#allprice").html(parseInt(allprice));
    }
    if(!isNaN(parseInt(limitPrice))){
      $("#limitPrice").html(parseInt(limitPrice));
    }
    if(!check){
      $("#allprice").html("0.00");
      $("#limitPrice").html("0.00");
      selectShopCartsIdList = [];
    }
  });
  /**
   * 合计选中商品总价
   *
   */
  function allprice() {
    selectShopCartsIdList =[];
    var arrayprice = $("#list").find(".mui-table-view-cell");
    var allprice = parseInt(0);
    var allLimitprice = parseInt(0);
    arrayprice.forEach(function (item,index,array) {
      var chec = $(item).find("input")[0];
      if(chec.checked){
        var shopcartsId = $(item).find(".checkbox")[0].getAttribute("shopcarts-id");
        selectShopCartsIdList.push(shopcartsId);
        var count =parseInt($(item).find(".value-num")[0].innerHTML);
        var privce = parseInt(item.getAttribute("data-price"));
        var limitPrice = parseInt(item.getAttribute("data-limitPrice"));
        allprice += parseInt(count*privce);
        allLimitprice += count*limitPrice;
      }
    });
    if(!isNaN(parseInt(allprice))){
      if(parseInt(allprice) == parseInt(0)){
        $("#allprice").html("0.00");
      }else{
        $("#allprice").html(parseInt(allprice));
      }
    }
    if(!isNaN(parseInt(limitPrice))){
      if(parseInt(limitPrice) == parseInt(0)){
        $("#limitPrice").html("0.00");
        return;
      }else{
        $("#limitPrice").html(parseInt(limitPrice));
      }
    }
  }

  /**
   * 选择
   *
   */
  $("#list").on("click",".checkbox",function () {
    allprice();
  });
  /**
   * 修改数量
   *
   */
  $("#list").on("click",".add-add",function () {
    var value = parseInt($(this).prev()[0].innerHTML) + 1;
    var aaa = $(this).prev()[0];
    var specificationId = $(this).prev()[0].getAttribute("data-specificationId");
    updataNum(value,specificationId,aaa);
    allprice();
  });
  $("#list").on("click",".add-num",function () {
    var value = parseInt($(this).next()[0].innerHTML) -1;
    if(value > 0){
      var specificationId = $(this).next()[0].getAttribute("data-specificationId");
      var a = $(this).next()[0];
      updataNum(value,specificationId,a);
    }
    allprice();
  });
  //删除购物车商品
  $("#list").on("click",".goods-delete",function () {
    var shopCartsId = $(this)[0].getAttribute("data-shopCartsId");
    var btnArray = ['删除', '取消'];
    mui.confirm('', '确定删除该条记录吗？', btnArray, function(e) {
      if(e.index == 0) {
        deteletaCart(shopCartsId);
      }
    });
  });
  function updataNum(count,specificationId,aaa) {
    $.ajax({
      type: 'POST',
      url: getUrl + '/wxmall/shopcarts/update',
      data: {
        "specificationId":specificationId,
        "memberId":memberId,
        "num":count
      },
      dataType: 'json',
      timeout: 15000,
      headers: headers("shopcarts.update"),
      context: $('body'),
      success: function(data){
        if(!data.success)
          mui.toast(data.msg);
        else
          aaa.innerHTML = count;
      },
      error: function(xhr, type){

      }
    });
  }

  /**
   * 删除购物车商品
   *
   * @param count
   */
  function deteletaCart(count) {
    $.ajax({
      type: 'POST',
      url: getUrl + '/wxmall/shopcarts/delete',
      data: {
        "shopCartsId":count,
        "memberId":memberId
      },
      dataType: 'json',
      headers: headers("shopcarts.delete"),
      timeout: 15000,
      success: function(data){
        deleteMsg(data);
      },
      error: function(xhr, type){

      }
    });
  }
  function deleteMsg(data) {
    if(data.success){
      mui.toast("删除成功");
      allprice();
      getCartList();
    }else{
      allprice();
      getCartList();
      mui.toast(data.msg);
    }
  }

  /**
   * 立即购买
   *
   */
  $(".footer-nav").on("click",".to-pay",function () {
    if(selectShopCartsIdList.length == 0){
      mui.toast("请选择商品");
    }else {
      // createOrder();
      // 确认订单页面需要使用
      sessionStorage.setItem("ShopCartsIdList",JSON.stringify(selectShopCartsIdList.unique3()));
      sessionStorage.setItem("toCartOrDetails","1");
      sessionStorage.setItem("allprice",$("#allprice").html())
      window.location.href="/usercenter/order/confirmOrder.html"
    }
  });
  // function createOrder() {
  //   var data = {
  //     "name":"哲大",
  //     "region":"浙江省杭州市西湖区",
  //     "address":"留下街道屏风新村",
  //     "mobile":"18298197706",
  //     "phone":"182",
  //     "idcard":"36234581736461727",
  //     "selectShopCartsIdList":selectShopCartsIdList.unique3()
  //   }
  //   $.ajax({
  //     type: 'POST',
  //     url: getUrl + '/wxmall/order/createOrder',
  //     data: JSON.stringify(data),
  //     dataType: 'json',
  //     timeout: 15000,
  //     headers: headers("order.createOrder"),
  //     contentType:'application/json',
  //     success: function(data){
  //       dispose(data);
  //     },
  //     error: function(xhr, type){
  //     }
  //   });
  // }
  // function dispose(data) {
  //   if(data.success){
  //     mui.toast(data.msg);
  //     window.location.href="/usercenter/order/confirmOrder.html"
  //   }else {
  //     mui.toast(data.msg);
  //   }
  //
  //
  //
  // }
  Array.prototype.unique3 = function(){
    var res = [];
    var json = {};
    for(var i = 0; i < this.length; i++){
      if(!json[this[i]]){
        res.push(this[i]);
        json[this[i]] = 1;
      }
    }
    return res;
  }
});